package demo.example.controller;

import demo.example.common.ApiRestResponse;
import demo.example.common.Constant;
import demo.example.common.PageRequest;
import demo.example.common.PageResult;
import demo.example.exception.TTExceptionEnum;
import demo.example.model.pojo.Message;
import demo.example.model.pojo.User;
import demo.example.service.MessageService;
import demo.example.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/excel")
public class ToExcelController {
    @Autowired
    MessageService messageService;
    @Autowired
    UserService userService;

    @GetMapping("/getAll")
    public ApiRestResponse getAll(HttpSession session){


        //从数据库获取所有数据
        List<Message> allMessage = messageService.getAllMessage();
//        if (session){
//
//        }
       //  通过session获取管理员用户 如果为空提示无管理员权限
        User attribute = (User) session.getAttribute(Constant.TT_USER);
        if(attribute == null){
            return ApiRestResponse.error(TTExceptionEnum.NEED_ADMIN);
        }else {
            // 查询用户是否为管理员
            User user = userService.selectByName(attribute.getUsername());
            if (StringUtils.isEmpty(user)){
                return ApiRestResponse.error(TTExceptionEnum.NOT_ADMIN);
            }else {
                return ApiRestResponse.success(allMessage);

            }
        }

    }
    @PostMapping("/selectAllMessage")
    public ApiRestResponse selectAllMessage(@RequestBody Map<String,Object> map ,HttpSession session){
        Map<String,String> message = (Map<String, String>)map.get("message");

        Message message1 = new Message(message.get("name"), message.get("sex"), message.get("phone"), message.get("remarks"));

        //从数据库获取所有数据
        List<Message> allMessage = messageService.getAllMessage(message1);
//        if (session){
//
//        }
        //  通过session获取管理员用户 如果为空提示无管理员权限
        User attribute = (User) session.getAttribute(Constant.TT_USER);
        if(attribute == null){
            return ApiRestResponse.error(TTExceptionEnum.NEED_ADMIN);
        }else {
            // 查询用户是否为管理员
            User user = userService.selectByName(attribute.getUsername());
            if (StringUtils.isEmpty(user)){
                return ApiRestResponse.error(TTExceptionEnum.NOT_ADMIN);
            }else {
                return ApiRestResponse.success(allMessage);

            }
        }
    }



    @PostMapping("/selectAll")
    public ApiRestResponse selectAll(@RequestBody Map<String,Object> map ,HttpSession session){
        Map<String,String> pageReq = (Map<String, String>) map.get("pageRequest");
        Map<String,String> message = (Map<String, String>)map.get("message");

        if (StringUtils.isEmpty(pageReq.get("pageNum")) || StringUtils.isEmpty(pageReq.get("pageSize"))){
            return ApiRestResponse.error(TTExceptionEnum.PAGE_ERROR);

        }

        PageRequest pageRequest = new PageRequest(Integer.parseInt(pageReq.get("pageNum")), Integer.parseInt(pageReq.get("pageSize")));
        Message message1 = new Message(message.get("name"), message.get("sex"), message.get("phone"), message.get("remarks"));

        //从数据库获取所有数据
        PageResult pageResult = messageService.selectAllByMessage(message1,pageRequest);

        //  通过session获取管理员用户 如果为空提示无管理员权限
        User attribute = (User) session.getAttribute(Constant.TT_USER);
        if(attribute == null){
            return ApiRestResponse.error(TTExceptionEnum.NEED_ADMIN);
        }else {
            // 查询用户是否为管理员
            User user = userService.selectByName(attribute.getUsername());
            if (StringUtils.isEmpty(user)){
                return ApiRestResponse.error(TTExceptionEnum.NOT_ADMIN);
            }else {
                return ApiRestResponse.success(pageResult);

            }
        }


    }

    @PostMapping("/deleteByPhone")
    public ApiRestResponse deleteByPhone(@RequestBody Map<String,Object> map  , HttpSession session){
        String phone = (String)map.get("phone");

        //  通过session获取管理员用户 如果为空提示无管理员权限
        User attribute = (User) session.getAttribute(Constant.TT_USER);
        if(attribute == null){
            return ApiRestResponse.error(TTExceptionEnum.NEED_ADMIN);
        }else {
            // 查询用户是否为管理员
            User user = userService.selectByName(attribute.getUsername());
            if (StringUtils.isEmpty(user)){
                return ApiRestResponse.error(TTExceptionEnum.NOT_ADMIN);
            }else {
                messageService.deleteByPhone(phone);
                return ApiRestResponse.success();

            }
        }
    }



}
